package cn.zwxky.android.user.Utils.chooseimg;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

import cn.zwxky.android.user.R;


public class MyAdapter extends CommonAdapter<String>
{

	private Context context;
	/**
	 * 閻€劍鍩涢柅澶嬪閻ㄥ嫬娴橀悧鍥风礉鐎涙ê鍋嶆稉鍝勬禈閻楀洨娈戠�瑰本鏆ｇ捄顖氱窞
	 */
	public static List<String> mSelectedImage  = new LinkedList<String>()  ;

	/**
	 * 閺傚洣娆㈡径纭呯熅瀵帮拷
	 */
	private String mDirPath;
	
	private int maxnum;

	public MyAdapter(Context context, List<String> mDatas, int itemLayoutId,
			String dirPath,int maxnum)
	{
		super(context, mDatas, itemLayoutId);
		this.mDirPath = dirPath;
		this.context=context;
		this.maxnum=maxnum;
	}

	@Override
	public void convert(final ViewHolder helper, final String item)
	{
		//鐠佸墽鐤唍o_pic
		helper.setImageResource(R.id.id_item_image, R.drawable.pictures_no);
		//鐠佸墽鐤唍o_selected
				helper.setImageResource(R.id.id_item_select,
						R.drawable.picture_unselected);
		//鐠佸墽鐤嗛崶鍓у
		helper.setImageByUrl(R.id.id_item_image, mDirPath + "/" + item);
		
		final ImageView mImageView = helper.getView(R.id.id_item_image);
		final ImageView mSelect = helper.getView(R.id.id_item_select);
		
		mImageView.setColorFilter(null);
		//鐠佸墽鐤咺mageView閻ㄥ嫮鍋ｉ崙璁崇皑娴狅拷
		mImageView.setOnClickListener(new OnClickListener()
		{
			//闁瀚ㄩ敍灞藉灟鐏忓棗娴橀悧鍥у綁閺嗘绱濋崣宥勭閸掓瑥寮芥稊锟�
			@Override
			public void onClick(View v)
			{

				// 瀹歌尙绮￠柅澶嬪鏉╁洩顕氶崶鍓у
				if (mSelectedImage.contains(mDirPath + "/" + item))
				{
					mSelectedImage.remove(mDirPath + "/" + item);
					mSelect.setImageResource(R.drawable.picture_unselected);
					mImageView.setColorFilter(null);
				} else
				// 閺堫亪锟藉瀚ㄧ拠銉ユ禈閻楋拷
				{
					if(mSelectedImage.size()<maxnum)
					{
					mSelectedImage.add(mDirPath + "/" + item);
					mSelect.setImageResource(R.drawable.pictures_selected);
					mImageView.setColorFilter(Color.parseColor("#77000000"));
					}else{
						Toast.makeText(context, "最多选中"+maxnum+"张", Toast.LENGTH_SHORT).show();
					}
				}

			}
		});
		
		/**
		 * 瀹歌尙绮￠柅澶嬪鏉╁洨娈戦崶鍓у閿涘本妯夌粈鍝勫毉闁瀚ㄦ潻鍥╂畱閺佸牊鐏�
		 */
		if (mSelectedImage.contains(mDirPath + "/" + item))
		{
			mSelect.setImageResource(R.drawable.pictures_selected);
			mImageView.setColorFilter(Color.parseColor("#77000000"));
		}

	}


}
